﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.Charts.Native;
using ECare.BUS.Workers;
using ECare.Client.templates;

using ECare.BUS.Data;
using ECare.BUS;

namespace ECare.Client.frms
{
    public partial class frmInInfo : frmInfo
    {
        public frmInInfo()
        {
            InitializeComponent();

            this.Load += frmInInfo_Load;

        }

        void frmInInfo_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();

            this.LoadResources();

            this.LoadInfomation();


            this.cbResource.SelectedIndexChanged+=cbResource_SelectedIndexChanged;

            this.txtNumber.ValueChanged += txtNumber_ValueChanged;
            this.txtUnitWeight.ValueChanged += txtUnitWeight_ValueChanged;
        }

        void txtUnitWeight_ValueChanged(object sender, EventArgs e)
        {
            this.UpdateTotalWeight();
        }

        void txtNumber_ValueChanged(object sender, EventArgs e)
        {
           this.UpdateTotalWeight();
        }

        private void UpdateTotalWeight()
        {
            //lblTotalWeighValue

            decimal unit=this.txtUnitWeight.Value;
            decimal numbers = this.txtNumber.Value;

            this.lblTotalWeighValue.Text = (unit*numbers).ToString("F2");
        }


        private void LoadInfomation()
        {
            if (this.entity == null)
            {
                this.txtBatId.Text = DateTime.Now.ToString("yyyyMMddhhmmss");

                this.txtProduceDate.Value = DateTime.Now;
            }
            else
            {
                var currentsupply = this.entity as SupplyInfo;
                this.txtBatId.Text = currentsupply.supply_code;
                this.cbSupply.SelectedValue = currentsupply.resource_supply_id;
                this.cbResource.SelectedValue = currentsupply.resource_id;
                this.txtDate.Value = currentsupply.supply_date;
                this.txtUnit.Text = currentsupply.supply_unit;
                this.txtUnitWeight.Value = currentsupply.supply_kg;
                this.txtInvalidDate.Value = currentsupply.supply_invalid_time;
                this.txtRemark.Text = currentsupply.supply_remark;
                this.txtProduceDate.Value = currentsupply.supply_produce_time == DateTime.MinValue ? DateTime.Now : currentsupply.supply_produce_time;
                this.txtNumber.Value = currentsupply.supply_number;


                this.txtSheetNo.Text = currentsupply.SheetNo;}
        }

        private void LoadResources()
        {
            this.cbResource.DataSource = ResourcesHelper.GetAllResources();
            this.cbResource.DisplayMember = "ResourceName";
            this.cbResource.ValueMember = "ResourceId";


            this.cbSupply.DataSource = ResourceSupplyHelper.GetResources();
            this.cbSupply.DisplayMember = "resource_supply_name";
            this.cbSupply.ValueMember = "resource_supply_id";

        }

        public event EntitySavedHandler Saved;

        private void btnSave_Click(object sender, EventArgs e)
        {
            #region


            if (!this.ValidateTextControl(this.txtSheetNo))
                return;


            if (!this.ValidateTextControl(this.txtBatId))
                return;

            if (this.cbResource.SelectedItem == null)
            {
                this.errorProvider1.SetError(this.cbResource, "?");
                return;
            }
            else
            {
                this.errorProvider1.SetError(this.cbResource, string.Empty);
            }

            if (this.txtNumber.Value == 0)
            {
                this.errorProvider1.SetError(this.txtNumber, "?");
                return;
            }
            else
            {
                this.errorProvider1.SetError(this.cbResource, string.Empty);
            }

            if (!this.ValidateTextControl(this.txtUnit))
                return;


            if (this.txtUnitWeight.Value == 0)
            {
                this.errorProvider1.SetError(this.txtUnitWeight, "?");
                return;
            }
            else
            {
                this.errorProvider1.SetError(this.txtUnitWeight, string.Empty);
            }

            #endregion


            if (this.entity == null)
            {
                var newsupply = new SupplyInfo();

                newsupply.supply_code = this.txtBatId.Text;
                newsupply.resource_supply_id = int.Parse(this.cbSupply.SelectedValue.ToString());
                newsupply.resource_id = int.Parse(this.cbResource.SelectedValue.ToString());
                newsupply.supply_date = this.txtDate.Value;
                newsupply.supply_number = (int)this.txtNumber.Value;
                newsupply.supply_unit = this.txtUnit.Text;
                newsupply.supply_kg = this.txtUnitWeight.Value;
                newsupply.supply_invalid_time = this.txtInvalidDate.Value;
                newsupply.supply_remark = this.txtRemark.Text;
                newsupply.supply_produce_time = this.txtProduceDate.Value;

                newsupply.supply_create_time = DateTime.Now;
                newsupply.supply_update_time = DateTime.Now;

                newsupply.SheetNo = this.txtSheetNo.Text;newsupply.supply_id = SupplyInfoHelper.CreateSupply(newsupply);



                this.entity = newsupply;
            }
            else
            {
                var newsupply = this.entity as SupplyInfo;

                newsupply.supply_code = this.txtBatId.Text;
                newsupply.resource_supply_id = int.Parse(this.cbSupply.SelectedValue.ToString());
                newsupply.resource_id = int.Parse(this.cbResource.SelectedValue.ToString());
                newsupply.supply_date = this.txtDate.Value;
                newsupply.supply_number = (int)this.txtNumber.Value;
                newsupply.supply_unit = this.txtUnit.Text;
                newsupply.supply_kg = this.txtUnitWeight.Value;
                newsupply.supply_invalid_time = this.txtInvalidDate.Value;
                newsupply.supply_remark = this.txtRemark.Text;
                newsupply.supply_produce_time = this.txtProduceDate.Value;
                newsupply.supply_update_time = DateTime.Now;


                newsupply.SheetNo = this.txtSheetNo.Text;
                SupplyInfoHelper.UpdateSupply(newsupply);
            }

            if (this.Saved != null)
            {
                this.Saved(this.entity);
                this.Close();
            }

        }

        private void eButton1_Click(object sender, EventArgs e)
        {

        }

        private void cbResource_SelectedIndexChanged(object sender, EventArgs e)
        {



            this.ChangeValidate();
        

        }

        private void ChangeValidate()
        {
            if (this.cbResource.SelectedItem == null)
                return;


            if (this.txtProduceDate.Value == DateTime.MinValue)
                return;


            ResoucesInfo selectedResources =
                ResourcesHelper.GetResourcesByResourceId(int.Parse(this.cbResource.SelectedValue.ToString()));



            this.txtInvalidDate.Value = this.txtProduceDate.Value.AddDays(selectedResources.ResouceValidate);


        }

        private void txtProduceDate_ValueChanged(object sender, EventArgs e)
        {
            this.ChangeValidate();
        }


    }
}
